package dingyunchi.job.controller;

import dingyunchi.job.entity.Course;
import dingyunchi.job.service.CourseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
public class CourseController {
    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping
    public String save(@RequestBody Course course) {
        try {
            Course savedCourse = courseService.save(course);
            return "课程添加成功，id为：" + savedCourse.getId();
        } catch (Exception e) {
            return "课程添加失败，原因：" + e.getMessage();
        }
    }

    @GetMapping("/{id}")
    public Course findById(@PathVariable Long id) {
        return courseService.findById(id);
    }

    @GetMapping
    public List<Course> findAll() {
        return courseService.findAll();
    }

    @PutMapping
    public String update(@RequestBody Course course) {
        try {
            courseService.update(course);
            return "课程信息修改成功";
        } catch (Exception e) {
            return "课程信息修改失败，原因：" + e.getMessage();
        }
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        try {
            courseService.delete(id);
            return "课程删除成功";
        } catch (Exception e) {
            return "课程删除失败，原因：" + e.getMessage();
        }
    }
}